<template>
  <div>
    <!--   <el-button type="primary" class="AddStudent">新增学生</el-button>-->
    <div>

    </div>


    <div class="container">
      <el-button type="primary" @click="dialogFormVisible = true" class="AddStudent"> 新增用户</el-button>

      <el-table :data="tableData" stripe style="margin-top: 10px; width: 100%; border: 1px;" class="StudentMessage">
        <el-table-column prop="userName" label="姓名" width="180"></el-table-column>
        <el-table-column prop="userSex" label="性别" width="180"></el-table-column>
        <el-table-column prop="userBir" label="生日" width="180"></el-table-column>
        <el-table-column prop="jobNumber" label="学号" width="180"></el-table-column>
        <el-table-column prop="identifyCard" label="身份证号"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!--        <el-popover placement="top" width="160" v-model="visible"><p>确认删除该用户</p>-->
            <!--          <div style="text-align: right; margin: 0;">-->
            <!--            <el-button size="mini" type="text" @click="visible = false" >取消</el-button>-->
            <!--            <el-button type="primary" size="mini" @click="visible = false">确定</el-button>-->
            <!--          </div>-->
            <el-button slot="reference" @click="deletes(scope.row.userId)" type="danger">删除</el-button>
            <!--        </el-popover>-->
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination style="margin-top: 20px;float: right"
                     background
                     layout="prev, pager, next"
                     :page-size="serrch.size"
                     :total="total"
                     @current-change="page">
      </el-pagination>

      <!--增加弹框-->
      <el-form :model="formAddUser">
        <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
          <el-form-item label="姓名" :label-width="formLabelWidth" style="width: 350px;margin-left: 200px">
            <el-input v-model="formAddUser.userName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="账号" :label-width="formLabelWidth" style="width: 350px;margin-left: 200px">
            <el-input v-model="formAddUser.account" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" :label-width="formLabelWidth" style="width: 350px;margin-left: 200px">
            <el-input v-model="formAddUser.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色" :label-width="formLabelWidth" style="width: 230px;margin-left: 200px;">
            <!--          <el-input v-model="" autocomplete="off" show-password></el-input>-->
            <el-select v-model="formAddUser.role" size="medium">
              <el-option label="老师" value="1">老师</el-option>
              <el-option label="学生" value="2">学生</el-option>
            </el-select>
          </el-form-item>
          <div slot="footer" class="dialog-footer">
<!--            <el-button type="primary">批量生成学生</el-button>-->
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="AddUser">确 定</el-button>
          </div>
        </el-dialog>
      </el-form>
    </div>

  </div>
</template>

<script>

import axios from "axios";

export default {
  name: "AllStudentList",
  data() {
    return {
      total: null,
      visible: false,
      tableData: [{
        userName: '',
        userSex: '',
        userBir: '',
        jobNumber: '',
        identifyCard: '',
      },],
      formAddUser: {
        userId: '',
        userName: '',
        account: '',
        password: '',
        role: '',
      },
      dialogFormVisible: false,
      forms: {
        delivery: false,
        type: [],
      },
      formLabelWidth: '120px',
      // page: {
      //   //一页大小
      //   pageSize: 6,
      //   //页数按钮的数量
      //   pagerCount: null,
      //   //一共多少条
      //   total: 0,
      //   //当前页数
      //   currentPage: ''
      // },

      serrch: {
        role: 2,
        page: 1,
        size: 5,
        userName: '',
      }
    }
  },
  methods: {
    init() {
      this.selectUser()
    },
    selectUser() {
      axios.post('http://localhost:8080/userinfo/findAllUserByRole', this.serrch).then(res => {
        this.tableData = res.data.data.content
        this.serrch.size = res.data.data.size
        this.total = res.data.data.total
        console.log(res)
      })
    },
    AddUser() {
      axios.post('http://localhost:8080/userinfo/add', this.formAddUser).then(res => {
        if (res.data.code == -1) {

          this.$message.warning("该账户已存在!")
        } else {
          this.$message.success("新增成功！")
        }
      })
    },
    deletes(userId) {
      console.log("删除");
      console.log(userId);
      axios.delete('http://localhost:8080/userinfo/delete?userId=' + userId).then(res => {
        console.log("删除成功");
        location.reload()
      })
    }, page(currentPage) {
      const _this = this;
      _this.serrch.page = currentPage
      axios.post('http://localhost:8080/userinfo/findAllUserByRole',_this.serrch).then(function (res) {
        _this.tableData = res.data.data.content
        _this.serrch.size = res.data.data.size
        _this.total = res.data.data.total
        console.log(res);
      })

    }
  }, created() {
    this.init()
  }
}
</script>

<style scoped>
.StudentMessage {

}

.container {
  width: 70%;
  margin-left: 240px;
  margin-top: 40px;
}

.AddStudent {
  margin-right: 1300px;
  margin-top: 10px;
}
</style>